<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('油品列表')"/>
    <th:block th:include="include :: bootstrap-editable-css"/>
    <th:block th:include="include :: jasny-bootstrap-css"/>
    <style>
        .wrap {
            position: relative;
        }

        .sm-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .xs-mark {
            position: absolute;
            top: 0;
            left: 0;
            margin: 0;
        }

        .sm-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 16px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .xs-mark:before {
            content: "";
            position: absolute;
            top: 0;
            left: 0;
            border-style: solid;
            border-width: 8px;
            z-index: 1;
            border-color: rgba(206, 3, 5, .7) transparent transparent rgba(206, 3, 5, .7);
        }

        .img-sm-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 20px;
            text-align: center;
        }

        .img-xs-text {
            color: #FFF;
            display: inline-block;
            position: absolute;
            top: 0;
            left: 0;
            z-index: 1;
            font-size: 12px;
            text-transform: uppercase;
            width: 0;
            text-align: center;
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="product-form">
                <div class="select-list">
                    <ul>
                        <li>
                            <p style="width: 80px">油品编号： </p>
                            <input type="text" name="productSn" value=""/>
                        </li>
                        <li>
                            <p style="width: 80px">油品名称： </p>
                            <input type="text" name="productName" value=""/>
                        </li>
                        <li>
                            <p style="width: 80px">状态： </p>
                            <select name="isEnable" th:with="type=${@dict.getType('product_status')}">
                                <option value="">全部</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <!--<li class="select-time">-->
                            <!--<p style="width: 80px">创建时间： </p>-->
                            <!--<input type="text" class="time-input" placeholder="开始时间"-->
                                   <!--name="params[beginTime]"/>-->
                            <!--<span>-</span>-->
                            <!--<input type="text" class="time-input" placeholder="结束时间"-->
                                   <!--name="params[endTime]"/>-->
                        <!--</li>-->
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="formReset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-primary multiple disabled" onclick="multiEdit(1,1)">
                <i class="fa fa-check"></i> 批量上架
            </a>
            <a class="btn btn-danger multiple disabled" onclick="multiEdit(1,0)">
                <i class="fa fa-ban"></i> 批量下架
            </a>
            <a class="btn btn-success" onclick="attrAdd()" shiro:hasPermission="back:shopProduct:attrAdd">
                <i class="fa fa-plus"></i> 新增油枪
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-table-editable-js"/>
<th:block th:include="include :: jasny-bootstrap-js"/>
<script th:inline="javascript">
    const prefix = ctx + "back/shopProduct";
    const attraddFlag  = [[${@permission.hasPermi('back:shopProduct:attrAdd')}]];
    const attrRemoveFlag  = [[${@permission.hasPermi('back:shopProduct:attrRemove')}]];

    $(function () {
        var options = {
            url: prefix + "/searchPetrolList",
            sortName: "orderNum asc,createTime",
            sortOrder: "desc",
            modalName: "油品",
            paginationShowPageGo: true,
            clickToSelect: true,
            onEditableSave: onEditableSave,
            detailView: true,
            onExpandRow: function (index, row, $detail) {
                initChildTable(index, row, $detail);
            },
            columns: [
                {
                    checkbox: true
                },
                {
                    field: 'id',
                    title: '油品ID',
                    visible:false
                },
                {
                    field: 'productSn',
                    title: '油品条码',
                    sortable: true
                },
                {
                    field: 'productCategory',
                    title: '油品类型',
                    sortable: true
                },
                {
                    field: 'productName',
                    title: '油品名称',
                    editable: {
                        type: 'text',
                        title: '油品名称',
                        emptytext: "【油品名称】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【油品名称】不能为空!';
                            }
                        }
                    },
                    sortable: true
                },
                {
                    field: 'productPrice',
                    title: '今日油价（元/升）',
                    editable: {
                        type: 'text',
                        title: '今日油价（元/升）',
                        emptytext: "【今日油价】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【今日油价】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【今日油价】必须是数字';
                            }
                            if (!(/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/.test(v))) {
                                return '请输入正确的【今日油价】';
                            }
                        }
                    }
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    sortable: true,
                    editable: {
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    sortable: true,
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "在售"
                        }, {
                            value: 0,
                            text: "停售"
                        }]
                    }
                }
                // {
                //     field: 'createTime',
                //     title: '创建时间',
                //     sortable: true
                // },
                // {
                //     title: '操作',
                //     align: 'center',
                //     formatter: function (value, row, index) {
                //         var actions = [];
                //         // actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.id + '\')"><i class="fa fa-trash"></i>删除</a>');
                //         return actions.join('');
                //     }
                // }
            ]
        };
        $.table.init(options);
    });

    initChildTable = function (index, row, $detail) {
        var cur_table = $detail.html('<table style="table-layout:fixed" id="attrTable'+row.id+'"></table>').find('table');
        $(cur_table).bootstrapTable({
            url: ctx + "back/shopProduct/searchAttrList",
            method: 'post',
            sidePagination: "server",
            contentType: "application/x-www-form-urlencoded",   // 编码类型
            queryParams: {
                searchType: 0,
                productId: row.id
            },
            onEditableSave: onEditableSave2,
            columns: [
                {
                    field: 'id',
                    title: 'ID',
                },
                {
                    field: 'attrName',
                    title: '油枪号',
                    editable: {
                        type: 'text',
                        title: '油枪号',
                        emptytext: "【油枪号】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【油枪号】不能为空!';
                            }
                        }
                    }
                },
                {
                    field: 'orderNum',
                    title: '排序',
                    editable: {
                        type: 'text',
                        title: '排序',
                        emptytext: "【排序】不能为空",
                        validate: function (v) {
                            if ($.trim(v) == '') {
                                return '【排序】不能为空!';
                            }
                            if (isNaN(v)) {
                                return '【排序】必须是数字';
                            }
                            if (!(/(^[0-9]\d*$)/.test(v))) {
                                return '请输入正确的【排序】';
                            }
                        }
                    }
                },
                {
                    field: 'isEnable',
                    title: '状态',
                    editable: {
                        type: 'select',
                        title: '状态',
                        source: [{
                            value: 1,
                            text: "启用"
                        }, {
                            value: 0,
                            text: "停用"
                        }]
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-danger btn-xs' + attrRemoveFlag + '" href="javascript:void(0)" onclick="attrRemove(\'' + row.id + '\',\''+row.productId+'\')"><i class="fa fa-trash"></i>删除</a>');
                        return actions.join('');
                    }
                }
            ]
        });
    };

    function onEditableSave(field, row) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.id = row.id;
        $.ajax({
            type: 'post',
            url: ctx + "back/shopProduct/petrolEdit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    function onEditableSave2(field, row) {
        let updateData = {};
        updateData[field] = row[field];
        updateData.id = row.id;
        $.ajax({
            type: 'post',
            url: ctx + "back/shopProduct/attrEdit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("修改成功");
                } else {
                    $.modal.alertError(result.msg);
                }
                $("#attrTable"+row.productId).bootstrapTable('refresh');
            }
        });
    }

    function multiEdit(type, val) {
        var rows = $.table.selectFirstColumns();
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        let updateData = {
            ids: rows.join()
        };
        switch (parseInt(type)) {
            case 1:
                //批量上架/下架
                updateData.isEnable = val;
                break;
            default:
                $.modal.alertWarning("操作失败！");
                return;
        }
        $.ajax({
            type: 'post',
            url: ctx + "back/shopProduct/edit",
            data: updateData,
            dataType: 'json',
            async: false,
            success: function (result) {
                if (result.code == web_status.SUCCESS) {
                    $.modal.alertSuccess("操作成功！");
                } else {
                    $.modal.alertError(result.msg);
                }
                $.table.refresh();
            }
        });
    }

    function attrAdd(){
        $.modal.open("新增油枪", "back/shopProduct/attrAdd");
    }

    function attrRemove(attrId,productId) {
        $.modal.confirm("删除油枪号将不能恢复，请确认？", function () {
            $.ajax({
                type: 'post',
                url: ctx + "back/shopProduct/attrRemove",
                data: {id:attrId},
                dataType: 'json',
                async: false,
                success: function (result) {
                    if (result.code == web_status.SUCCESS) {
                        $.modal.alertSuccess("操作成功！");
                    } else {
                        $.modal.alertError(result.msg);
                    }
                    $("#attrTable"+productId).bootstrapTable('refresh');
                }
            });
        });
    }

    function formReset() {
        $('input[name="categoryIdStr"]').val("");
        $.form.reset();
    }
</script>
</body>
</html>